<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->

          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list" border>
              <el-table-column label="序号" width="50" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignPermissions(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">

              <el-pagination
                :current-page="pagParams.page"
                :page-sizes="[2, 3, 5, 10 , 20]"
                :page-size="pagParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />

              <el-pagination layout="prev,pager,next" />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        title="新增或者编辑角色"
        :visible.sync="dialogFormVisible"
        width="500px"
        @closed="resetForm"
      >
        <el-form ref="ruleForm" :model="form" :rules="rules">

          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" autocomplete="off" />
          </el-form-item>

          <el-form-item label="角色描述" prop="description">
            <el-input v-model="form.description" autocomplete="off" />
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="hSubmit">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog
        title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
        :visible.sync="perDialog"
      >
        <assign-permission
          v-if="perDialog"
          :cur-id="curId"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import assignPermission from './assignPermission'
import { addSetting, deleSetting, getSetting, updatSetting } from '@/api/setting'
export default {
  name: 'Settings',
  components: {
    assignPermission
  },

  data() {
    return {
      curId: '',
      perDialog: false,
      list: [],
      pagParams: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      form: {
        name: '',
        description: ''
      },
      dialogFormVisible: false,
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      isEdit: false
    }
  },

  mounted() {

  },
  created() {
    this.loadRoles()
  },
  methods: {
    resetForm() {
      this.$refs.ruleForm.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    // 获得数据
    async loadRoles() {
      try {
        const res = await getSetting(this.pagParams)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pagParams.page = 1
      this.pagParams.pagesize = val
      this.loadRoles()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pagParams.page = val
      this.loadRoles()
    },
    // 打开弹窗页面
    openDialog() {
      this.dialogFormVisible = true
    },
    hSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    async doAdd() {
      try {
        const res = await addSetting(this.form)
        if (res.code === 10000) {
          this.$message.success(res.message)
          this.total++
          const maxPage = Math.ceil(this.total / this.pagParams.pagesize)
          this.pagParams.page = maxPage
          this.loadRoles()
          this.dialogFormVisible = false
        } else {
          this.$message.error(res.message)
        }
      } catch (error) {
        console.log(error)
      }
    },
    hEdit(row) {
      console.log(row)
      this.form = { ...row }
      this.isEdit = true
      this.dialogFormVisible = true
    },
    hAdd() {
      this.isEdit = false
      this.dialogFormVisible = true
    },
    async doEdit() {
      try {
        const res = await updatSetting(this.form)
        if (res.code === 10000) {
          this.$message.success(res.message)
          if (this.li.length === 1) {
            this.pagParams.page--
            if (this.pagParams.page <= 0) {
              this.pagParams.page = 1
            }
          }
        } else {
          this.$message.error(res.message)
        }
        this.dialogFormVisible = false
      } catch (error) {
        console.log(error)
      }
    },
    async hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleSetting(id)
        if (res.code === 10000) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loadRoles()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    assignPermissions(id) {
      this.curId = id
      this.perDialog = true
    }
  }
}
</script>

<style lang="sass" scoped>

</style>
